package com.yt.lxh.acl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yt.lxh.acl.service.RoleService;
import com.yt.lxh.common.result.Result;
import com.yt.lxh.common.result.ResultCodeEnum;
import com.yt.lxh.dto.role.RoleDto;
import com.yt.lxh.model.acl.Role;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 角色管理
 */
@RequestMapping("/admin/acl/role")
@Api(tags = "角色管理")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;

    @ApiOperation(value = "角色分页列表")
    @PostMapping("/findByPage/{current}/{limit}")
    public Result findByPage(
            @ApiParam(name = "current", value = "当前页码", required = true)
            @PathVariable Integer current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Integer limit,
            @RequestBody RoleDto roleDto) {
        Page<Role> pageParams = new Page<>(current, limit);
        IPage<Role> pageModel = roleService.selectRolePage(pageParams, roleDto);
        return Result.success(pageModel);
    }

    @ApiOperation(value = "添加角色")
    @PostMapping("save")
    public Result save(@RequestBody Role role,
                       @RequestHeader(value = "token") String token) {
        roleService.saveRole(role,token);
        return Result.success(null);
    }

    @ApiOperation(value = "获取角色")
    @GetMapping("get/{id}")
    public Result get(@PathVariable Long id) {
        Role role = roleService.getById(id);
        return Result.success(role);
    }

    @ApiOperation(value = "修改角色")
    @PutMapping("update")
    public Result updateById(@RequestBody Role role) {
        roleService.updateById(role);
        return Result.success(null);
    }

    @ApiOperation(value = "删除角色")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable Long id) {
        roleService.removeById(id);
        return Result.success(null);
    }

    @ApiOperation(value = "根据id列表删除角色")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> idList) {
        roleService.removeByIds(idList);
        return Result.success(null);
    }

    @ApiOperation(value = "查询所有角色")
    @GetMapping("/findAllRoles/{adminId}")
    public Result findAllRoles(@PathVariable("adminId") Integer adminId){
        Map<String,Object> map = roleService.findAllRoles(adminId);
        return Result.build(map, ResultCodeEnum.SUCCESS);
    }
}
